<?php
include_once 'library/Entity/EntityInterface.php';
class BaseEntity implements EntityInterface{

/*-------------------------------COLUMS-------------------------------*/	
	const CREATEBY = 'createby';
	const CREATENAME = 'createname';
	const CREATETIME = 'createtime';
	const LASTUPDATEBY = 'lastupdateby';
	const LASTUPDATENAME = 'lastupdatename';
	const LASTUPDATETIME = 'lastupdatetime';
	const ROWVERSION = 'rowversion';
	const STATUS = 'status';
	const STATUSNAME = 'statusname';
	const DEFAULT_STATUS = 1;
	const ID = 'id';
	const FIRST_INT_SEQUENCE = 1;
	const MAX_PREFIX = "max";
	const FIRST_ID = 1;
	const ERROR_TYPE_RESOURCE = 'resource';
	const ERROR_TYPE_STRING = 'string';
	/**
	 * DB Object.
	 * @var DBHelper
	 */
	protected $dbHelper;
	/**
	 * Error Log.
	 * @var array
	 */
	private $_error = array();
	/**
	 * Primary key ...
	 * @var array
	 */
	public $pkey = array();
	/**
	 * Auto increment column.
	 * @var string
	 */
	public $sequence = STRING_EMPTY;
	/**
	 * Unique column.
	 * @var array
	 */
	public $unique = array();
	
	public $unsetCols = array(
		BaseEntity::CREATENAME,
		BaseEntity::LASTUPDATENAME,
		BaseEntity::STATUSNAME
	);

	public function __construct(){
		$this->dbHelper = DBHelper::getInstance();
	}
	/**
	 * Return ID Array.
	 * @return array().
	 */
	public function ID(){
		return array('id');
	}
	/**
	 * return next ID Sequece.
	 */
	public function getNextID(){
		$mkey = "maxID";
		$sql = 'select max(CAST(id AS UNSIGNED)) as '.$mkey.' from '.$this->getTable();
		$max = $this->dbHelper->fetchRow($sql);
		if(isset($max[$mkey])) return array(BaseEntity::ID=>$max[$mkey] + 1);
		else return $this->FirstID();
	}
	/**
	 * return First ID Sequece.
	 */
	public function FirstID(){
		return array(BaseEntity::ID=>1);
	}
	/**
	 * return Last ID Use.
	 */
	public function LastUseID(){
		$mkey = "maxID";
		$sql = 'select max(id) as '.$mkey.' from '.$this->getTable();
		$max = $this->dbHelper->fetchRow($sql);
		if(isset($max[$mkey])) return $max[$mkey];
		else return $this->FirstID();
	}
	/**
	 * Get all Entity column in DB.
	 * @return array
	 */
	public function getAll(){
		$sql = 'select * from ' . $this->getTable();
		return $this->dbHelper->fetchAll($sql);
	}
	/**
	 * Get all Entity column in DB.
	 * @return array
	 */
	public function getAllWhere($where){
		$sql = 'select * from ' . $this->getTable() . ' where '.$where;
		return $this->dbHelper->fetchAll($sql);
	}
	/**
	 * Get some Entity column in DB.
	 * @return array
	 */
	public function getAllByCols($cols){
		$sql = 'select '.$this->getCol($cols).' from ' . $this->getTable();
		return $this->dbHelper->fetchAll($sql);
	}
	/**
	 * Single Entity(all column) by ID not have (:).<br>
	 * Joint Default Table.
	 * @param array $id
	 * @return array or null if exist or not exist.
	 */
	public function getByIDJoin(array $id){
		$sql = 'select '.$this->getJointDefault().' where '.$this->getIDWhereT($id);
		return $this->dbHelper->fetchRow($sql);
	}
	/**
	 * Single Entity(all column) by ID have (:).<br>
	 * Joint Default Table.
	 * @param array $id
	 * @return array or null if exist or not exist.
	 */
	public function getByIDJoin1(array $id){
		$sql = 'select '.$this->getJointDefault().' where '.$this->getIDWhere1T($id);
		return $this->dbHelper->fetchRow($sql);
	}
	/**
	 * Single Entity(all column) by ID not have (:) with not join.
	 * @param array $id
	 * @return array or null if exist or not exist.
	 */
	public function getByID(array $id){
		$sql = 'select * from '.$this->getTable().' where '.$this->getIDWhere($id);
		return $this->dbHelper->fetchRow($sql);
	}
	/**
	 * Single Entity(all column) by ID not have (:) with join.
	 * @param array $id
	 * @return array or null if exist or not exist.
	 */
	public function getByIDT(array $id){
		$sql = 'select t.*, 
				c.name as '.BaseEntity::CREATENAME.',
				l.name as '.BaseEntity::LASTUPDATENAME.',
				s.name as '.BaseEntity::STATUSNAME.'
				from '.$this->getTable().' as t 
				LEFT JOIN usersystem as c ON t.createby = c.id
				LEFT JOIN usersystem as l ON t.lastupdateby = l.id
				LEFT JOIN status as s ON t.status = s.id
				 where '.$this->getIDWhereT($id);
		return $this->dbHelper->fetchRow($sql);
	}
	/**
	 * Single Entity(all column) by ID have (:).
	 * @param array $id
	 */
	public function getByID1(array $id){
		$sql = 'select * from '.$this->getTable().' where '.$this->getIDWhere1($id);
		return $this->dbHelper->fetchRow($sql);
	}
	
	public function getJointDefault(){
		$str = 't.*,createbyuser.name as '.BaseEntity::CREATENAME ;
		$str .= ',updatebyuser.name as '.BaseEntity::LASTUPDATENAME;
		$str .= ',s.name as '.BaseEntity::STATUSNAME;
		$str .= ' from ' . $this->getTable() . ' as t';
		$str .= ' left join usersystem as createbyuser on ';
		$str .= 't.'.BaseEntity::CREATEBY . ' = createbyuser.id ';
		$str .= 'left join usersystem as updatebyuser on ';
		$str .= 't.'.BaseEntity::LASTUPDATEBY . ' = updatebyuser.id ';
		$str .= ' left join status as s on';
		$str .= ' s.id = t.'.BaseEntity::STATUS;
		return $str;
	}
	/**
	 * Single Entity(some column) by ID not have (:).
	 * @param array $id
	 * @param array $cols
	 */
	public function getByIDByCols(array $id,array $cols){
		$sql = 'select '.$this->getCol($cols).' from '.$this->getTable().' where '.$this->getIDWhere($id);
		return $this->dbHelper->fetchRow($sql);
	}

	/**
	 * Single Entity(some column) by ID have(:).
	 * @param array $id
	 * @param array $cols
	 */
	public function getByIDByCols1(array $id,array $cols){
		$sql = 'select '.$this->getCol($cols).' from '.$this->getTable().' where '.$this->getIDWhere1($id);
		return $this->dbHelper->fetchRow($sql);
	}
	/**
	 * All Entity(get all column) by Fields not have(:).
	 * @param array $id
	 */
	public function getByFields(array $fields){
		$sql = 'select * from '.$this->getTable().' where '.$this->getWhere($fields);
		return $this->dbHelper->fetchAll($sql);
	}

	/**
	 * All Entity(get all column) by Fields have(:).
	 * @param array $id
	 */
	public function getByFields1(array $fields){
		$sql = 'select * from '.$this->getTable().' where '.$this->getWhere1($fields);
		return $this->dbHelper->fetchAll($sql);
	}
	/**
	 * All Entity(get some column) by Fields not have(:).
	 * @param array $id
	 * @param array $cols
	 */
	public function getByFieldsByCols(array $fields,array $col){
		$sql = 'select '.$this->getCol($col).' from '.$this->getTable().' where '.$this->getWhere($fields);
		return $this->dbHelper->fetchAll($sql);
	}
	/**
	 * All Entity(get some column) by Fields have(:).
	 * @param array $id
	 * @param array $cols
	 */
	public function getByFieldsByCols1(array $fields,array $col){
		$sql = 'select '.$this->getCol($col).' from '.$this->getTable().' where '.$this->getWhere1($fields);
		return $this->dbHelper->fetchAll($sql);
	}
/*------------------------------------END - GET-----------------------------------------------*/
/*----------------------------INSERT - UPDATE - DELETE------------------------------------------------------------*/
	/**
	 * Insert into table with enity not have(:)<br>
	 * validate data<br>
	 * return 1 or 0 as success or fail.
	 * @param array $entity
	 * @return int 1 or 0 with success or fail
	 */
	public function insert(array $entity){
		if($this->isExist($entity)){
			$this->addError(Resource::MES_ER_INSERT_EXIST,BaseEntity::ERROR_TYPE_RESOURCE);
			return DBHelper::FAIL;
		}
		foreach ($this->unique as $value) {
			if($this->isExistCol($entity, $value)){
				$this->addError(Resource::MES_ER_INSERT_COL_EXIST, BaseEntity::ERROR_TYPE_RESOURCE);
				return DBHelper::FAIL;
			} 
		}
		if($this->nextID()>0){
			$entity[$this->sequence] = $this->nextID();
		}
		return $this->insertSimple($entity);
	}
	/**
	 * Insert into table with enity have(:)<br>
	 * validate data<br>
	 * return 1 or 0 as success or fail.
	 * @param array $entity
	 */
	public function insert1(array $entity){
		if($this->isExist1($entity)){
			$this->addError(Resource::MES_ER_INSERT_EXIST,BaseEntity::ERROR_TYPE_RESOURCE);
			return DBHelper::FAIL;
		}
		foreach ($this->unique as $value) {
			if($this->isExistCol1($entity, $value)){
				$this->addError(Resource::MES_ER_INSERT_COL_EXIST,BaseEntity::ERROR_TYPE_RESOURCE);
				return DBHelper::FAIL;
			}
		}
		if($this->nextID()>0){
			$akey = ':'.$this->sequence;
			$entity[$akey] = $this->nextID();
		}
		return $this->insertSimple1($entity);
	}
	/**
	 * Simple insert with not have(:)<br>
	 * Insert with no validate data
	 * @param array $entity
	 * @return 1/0 if Success or fail.
	 */
	public function insertSimple(array $entity){
		$sql = 'insert into '.$this->getTable().' '
		.$this->getInsertDeclare($entity)
		.' values '.$this->getInsertValues($entity);
		$result = $this->dbHelper->executeArray($sql, $entity);
		if($result != DBHelper::SUCCESS){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Simple insert with have(:)<br>
	 * Insert with no validate data
	 * @param array $entity
	 * @return 1/0 if Success or fail.
	 */
	public function insertSimple1(array $entity){
		$sql = 'insert into '.$this->getTable().' '
		.$this->getInsertDeclare1($entity)
		.' values '.$this->getInsertValues1($entity);
		$result = $this->dbHelper->executeArray($sql, $entity);
		if($result != DBHelper::SUCCESS){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Update with not have(:)<br>
	 * Update data check exist ID<br>
	 * Check changed columns
	 * @param array $entity
	 * @return 1/0 if success or fail
	 */
	public function update(array $entity){
		$sets = $this->getUpdateSets($entity);
		if($sets == ''){
			$this->addError(Resource::MES_ER_UPDATE_NON, BaseEntity::ERROR_TYPE_RESOURCE);
			return DBHelper::FAIL;
		}
		$sql = 'update '.$this->getTable().' set '.$sets.' where '.$this->getIDWhere($entity);
		$result = $this->dbHelper->executeArray($sql, $entity);
		if($result != DBHelper::SUCCESS){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Update with have(:)<br>
	 * Update data check exist ID<br>
	 * Check changed columns
	 * @param array $entity
	 * @return 1/0 if success or fail
	 */
	public function update1(array $entity){
		$sets = $this->getUpdateSets1($entity);
		if($sets == ''){
			$this->addError(Resource::MES_ER_UPDATE_NON, BaseEntity::ERROR_TYPE_RESOURCE);
			return DBHelper::FAIL;
		}
		$sql = 'update '.$this->getTable().' set '.$sets.' where '.$this->getIDWhere1($entity);
		$result = $this->dbHelper->executeArray($sql, $entity);
		if($result != DBHelper::SUCCESS){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Simple update not have(:)
	 * Not check exist.
	 * @param array $entity
	 * @return true or false if success or fail
	 */
	public function updateSimple(array $entity){
		$sql = 'update '.$this->getTable().' set '.$this->getUpdateSets($entity).' where '.$this->getIDWhere($entity);
		$result = $this->dbHelper->executeArray($sql, $entity);
		if($result != DBHelper::SUCCESS){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Simple update have(:)
	 * Not check exist.
	 * @param array $entity
	 * @return true or false if success or fail
	 */
	public function updateSimple1(array $entity){
		$sql = 'update '.$this->getTable().' set '.$this->getUpdateSets1($entity).' where '.$this->getIDWhere1($entity);
		$result = $this->dbHelper->executeArray($sql, $entity);
		if($result != DBHelper::SUCCESS){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Delete with not have(:)<br>
	 * delete entity where ID and return return the number of affected rows. 
	 * @param array $entity
	 * @return int
	 */
	public function delete(array $entity){
		$sql = 'delete from '.$this->getTable().' where '.$this->getIDWhere($entity);
		$result = $this->dbHelper->execute($sql);
		if($result < 1){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
	/**
	 * Delete with have(:)<br>
	 * delete entity where ID and return return the number of affected rows. 
	 * @param array $entity
	 * @return int
	 */
	public function delete1(array $entity){
		$sql = 'delete from '.$this->getTable().' where '.$this->getIDWhere1($entity);
		$result = $this->dbHelper->execute($sql);
		if($result < 1){
			$this->addError($this->dbHelper->getError(), BaseEntity::ERROR_TYPE_STRING);
		}
		return $result;
	}
/*-----------------------------------GET - SET STRING FOR INSERT - UPDATE - DELETE------------------------------------------------------------*/	
	/**
	 * Check Exis entity by ID not have(:)<br>
	 * Return true or false if exist or not exist.
	 * @param array $entity
	 * @return true/false
	 */
	public function isExist(array $entity){
		if(count($this->pkey)>0){
			$sql = 'select '.$this->getCols($entity).' from '.$this->getTable().' where '.$this->getIDWhere($entity);
			$result = $this->dbHelper->fetchAll($sql);
			return (count($result)>0) ? true : false;
		}
		return false;
	}

	/**
	 * Check Exis entity by ID have(:)<br>
	 * Return true or false if exist or not exist.
	 * @param array $entity
	 * @return true/false
	 */
	public function isExist1(array $entity){
		if(count($this->pkey)>0){
			$sql = 'select '.$this->getCols1($entity).' from '.$this->getTable().' where '.$this->getIDWhere1($entity);
			$result = $this->dbHelper->fetchAll($sql);
			return (count($result)>0) ? true : false;
		}
		return false;
	}
	/**
	 * Check Exis entity by columns not have(:)<br>
	 * Return true or false if exist or not exist<br>
	 * Using in check unique key.
	 * @param array $entity
	 * @return true/false
	 */
	public function isExistCol(array $entity,$colname){
		$sql = 'select '.$colname.' from '.$this->getTable().' where '.$colname.'= "'.$entity[$colname].'"';
		$result = $this->dbHelper->fetchAll($sql);
		return (count($result)>0) ? true : false;
	}
	/**
	 * Check Exis entity by columns have(:)<br>
	 * Return true or false if exist or not exist<br>
	 * Using in check unique key.
	 * @param array $entity
	 * @return true/false
	 */
	public function isExistCol1(array $entity,$colname){
		$keyset = ':'.$colname;
		$sql = 'select '.$colname.' from '.$this->getTable().' where '.$colname.'= "'.$entity[$keyset].'"';
		$result = $this->dbHelper->fetchAll($sql);
		return (count($result)>0) ? true : false;
	}
	/**
	 * get select column from array not have (:) and not have key.
	 * @param array $cols
	 */
	public function getCol(array $entity){
		$select = "";
		$first = true;
		foreach ($entity as $key) {
			if($first){
				$select .= $key;
				$first = false;
			}
			else{
				$select .= ','.$key;
			}
		}
		return $select;
	}
	/**
	 * get select column from array not have (:).
	 * @param array $cols
	 */
	public function getCols(array $entity){
		$select = "";
		$first = true;
		foreach ($entity as $key => $value) {
			if($first){
				$select .= $key;
				$first = false;
			}
			else{
				$select .= ','.$key;
			}
		}
		return $select;
	}
	/**
	 * get select column from array have (:).
	 * @param array $cols
	 */
	public function getCols1(array $entity){
		$select = "";
		$first = true;
		foreach ($entity as $key => $value) {
			$akey = substr($key, 1);
			if($first){
				$select .= $akey;
				$first = false;
			}
			else{
				$select .= ','.$akey;
			}
		}
		return $select;
	}
	/**
	 * Return Entity where string by IDs Fields not have (:).
	 * @param array $entity
	 */
	public function getIDWhere(array $entity){
		$where = '';
		if(count($this->pkey)>0){
			$first = true;
			foreach ($this->pkey as $key) {
				if($first){
					$where .= $key. ' = "'.$entity[$key].'"';
					$first = false;
				}
				else{
					$where .= ' and '.$key. ' = "'.$entity[$key].'"';
				}
			}
		}
		return $where;
	}
	/**
	 * t.column <br>
	 * Return Entity where string by IDs Fields not have (:).
	 * @param array $entity
	 */
	public function getIDWhereT(array $entity){
		$where = '';
		if(count($this->pkey)>0){
			$first = true;
			foreach ($this->pkey as $key) {
				if($first){
					$where .= 't.'.$key. ' = "'.$entity[$key].'"';
					$first = false;
				}
				else{
					$where .= ' and t.'.$key. ' = "'.$entity[$key].'"';
				}
			}
		}
		return $where;
	}
	/**
	 * Return Entity where string by IDs Fields have (:).
	 * @param array $entity
	 */
	public function getIDWhere1(array $entity){
		$where = '';
		if(count($this->pkey)>0){
			$first = true;
			foreach ($this->pkey as $key) {
				$akey = ':'.$key;
				if($first){
					$where .= $key. ' = "'.$entity[$akey].'"';
					$first = false;
				}
				else{
					$where .= ' and '.$key. ' = "'.$entity[$akey].'"';
				}
			}
		}
		return $where;
	}
	/**
	 * Return Entity where string by IDs Fields have (:).
	 * @param array $entity
	 */
	public function getIDWhere1T(array $entity){
		$where = '';
		if(count($this->pkey)>0){
			$first = true;
			foreach ($this->pkey as $key) {
				$akey = ':'.$key;
				if($first){
					$where .= 't.'.$key. ' = "'.$entity[$akey].'"';
					$first = false;
				}
				else{
					$where .= ' and t.'.$key. ' = "'.$entity[$akey].'"';
				}
			}
		}
		return $where;
	}
	/**
	 * get where condition from array not have (:).
	 * @param array $cols
	 */
	public function getWhere(array $cols){
		$where = "";
		$first = true;
		foreach ($cols as $key => $value) {
			if($first){
				$where .= $key. ' = "'.$value.'"';
				$first = false;
			}
			else{
				$where .= ' and '.$key. ' = "'.$value.'"';
			}
		}
		return $where;
	}
	/**
	 * get where condition from array have (:).
	 * @param array $cols
	 */
	public function getWhere1(array $cols){
		$where = "";
		$first = true;
		foreach ($cols as $key => $value) {
			$akey = substr($key, 1);
			if($first){
				$where .= $akey. ' = "'.$value.'"';
				$first = false;
			}
			else{
				$where .= ' and '.$akey. ' = "'.$value.'"';
			}
		}
		return $where;
	}
	/**
	 * Return string declaretion befor insert query with not have(:)<br>
	 * sample insert into table "declaretion"<br>
	 * sample declaretion = (id,name,pass).
	 * @param array $entity
	 */
	public function getInsertDeclare(array $entity){
		$declare = "(";
		$first = true;
		foreach ($entity as $key => $value) {
			if($first){
				$declare .= $key;
				$first = false;
			}
			else {
				$declare .= ','.$key;
			}
		}
		$declare .= ")";
		return $declare;
	}
	/**
	 * Return string declaretion befor insert query with have(:)<br>
	 * sample insert into table "declaretion"<br>
	 * sample declaretion = (id,name,pass).
	 * @param array $entity
	 */
	public function getInsertDeclare1(array $entity){
		$declare = "(";
		$first = true;
		foreach ($entity as $key => $value) {
			$akey = substr($key, 1);
			if($first){
				$declare .= $akey;
				$first = false;
			}
			else {
				$declare .= ','.$akey;
			}
		}
		$declare .= ")";
		return $declare;
	}
	/**
	 * Return string values befor insert query with not have(:)<br>
	 * sample insert into table (id,name,pass) values "values"<br>
	 * sample values = (:id,:name,:pass).
	 * @param array $entity
	 */
	public function getInsertValues(array $entity){
		$values = "(";
		$first = true;
		foreach ($entity as $key => $value) {
			$akey = ':'.$key;
			if($first){
				$values .= $akey;
				$first = false;
			}
			else {
				$values .= ','.$akey;
			}
		}
		$values .= ")";
		return $values;
	}
	/**
	 * Return string values befor insert query with have(:)<br>
	 * sample insert into table (id,name,pass) values "values"<br>
	 * sample values = (:id,:name,:pass).
	 * @param array $entity
	 */
	public function getInsertValues1(array $entity){
		$values = "(";
		$first = true;
		foreach ($entity as $key => $value) {
			if($first){
				$values .= $key;
				$first = false;
			}
			else {
				$values .= ','.$key;
			}
		}
		$values .= ")";
		return $values;
	}
	/**
	 * Run with not have(:)<br>
	 * Check change value and return set string befor update<br>
	 * sample name= "abc",phone="1234567890"
	 * @param array $entity
	 * @return string
	 */
	public function getUpdateSets(array $entity){
		$entity = $this->unsetDefaultJoin($entity);
		$sets = '';
		$partial = $this->getByID($entity);
		if(isset($partial)){
			$first = true;
			$entity[BaseEntity::ROWVERSION] = $partial[BaseEntity::ROWVERSION] + 1; 
			foreach ($partial as $key => $value) {
				if(isset($entity[$key])&&$entity[$key] != $value){
					if($first){
						$sets .= $key . "='".$entity[$key]."'";	
						$first = false;
					}
					else{
						$sets .= ','.$key . "='".$entity[$key]."'";
					}
				}
			}
		}
		return $sets;
	}
	public function unsetDefaultJoin($entity){
		foreach ($this->unsetCols as $value) {
			unset($entity[$value]);
		}
		return $entity;
	}
	public function unsetDefaultJoin1($entity){
		foreach ($this->unsetCols as $value) {
			unset($entity[':'.$value]);
		}
		return $entity;
	}
	/**
	 * Run with not have(:)<br>
	 * Check change value and return set string befor update<br>
	 * sample name= "abc",phone="1234567890"
	 * @param array $entity
	 * @return string
	 */
	public function getUpdateSets1(array $entity){
		$entity = $this->unsetDefaultJoin1($entity);
		$sets = '';
		$partial = $this->getByID1($entity);
		if(isset($partial)){
			$first = true;
			foreach ($partial as $key => $value) {
				$akey = ':'.$key;
				if($entity[$akey] != $value){
					if($first){
						$sets .= $key . '="'.$entity[$akey].'"';	
						$first = false;
					}
					else{
						$sets .= ','.$key . '="'.$entity[$akey].'"';
					}
				}
			}
		}
		return $sets;
	}
	/**
	 * Return next sequence key.
	 * @return int
	 */
	public function nextID(){
		if($this->sequence != STRING_EMPTY){
			return $this->nextSequence($this->sequence);
		}
		return 0;
	}
	/**
	 * Return next sequence key by column.
	 * @return int
	 * @param string $col
	 */
	public function nextSequence($col){
		$mkey = BaseEntity::MAX_PREFIX.$col;
		$sql = 'select max(CAST('.$col.' AS UNSIGNED)) as '.$mkey.' from '.$this->getTable();
		$max = $this->dbHelper->fetchRow($sql);
		if(isset($max[$mkey])) return $max[$mkey] + 1;
		else return BaseEntity::FIRST_ID;
	}
	/**
	 * Return next integer sequence key by column.
	 * @return int
	 * @param string $col
	 */
	public function nextIntSequence($col){
		$mkey = BaseEntity::MAX_PREFIX.$col;
		$sql = 'select max('.$col.') as '.$mkey.' from '.$this->getTable();
		$max = $this->dbHelper->fetchRow($sql);
		if(isset($max[$mkey])) return $max[$mkey] + 1;
		else return BaseEntity::FIRST_INT_SEQUENCE;
	}
	/**
	 * Check Unique value exist in current database <br>
	 * Return true/fale if exist or not.
	 * Enter description here ...
	 * @param String $col
	 * @param String $value
	 * @return true or false
	 */
	public function checkUnique($col,$value){
		$sql = 'select '.$col.' from '.$this->getTable().' where '.$col.'="'.$value.'"';
		if($this->dbHelper->fetchCount($sql)>0)
			return true;
		return false;
	}

	/*------------------------------------GET - SET-----------------------------------------------*/
	/**
	 * Return Entity table name.
	 * @return string
	 */
	public function getTable(){
		return strtolower(get_class($this));
	}
/**
	 * Return Entity table name.
	 * @return string
	 */
	public static function Table(){
		return strtolower(get_class($this));
	}
	/**
	 * Return Entity Class ...
	 * @return string
	 */
	public function getClass(){
		return get_class($this);
	}

	public function getError(){
		return $this->_error;
	}
	public function setError($error){
		$this->_error = $error;
	}
	/**
	 * Add error to list error<br>
	 * Input resource error key.
	 * @param string $error error key or string
	 * @param string $type error type resource or string
	 */
	public function addError($error,$type){
		array_push($this->_error, array('type'=>$type,'value'=>$error));
	}
	public function getErrorsMessage(){
		$messages = array();
		foreach ($this->_error as $value){
			$msg = "";
			if($value["type"] == BaseEntity::ERROR_TYPE_RESOURCE){
				$msg = Resource::getInstance()->getResource($value["value"]);
			}
			else{
				$msg = $value["value"];
			}
			array_push($messages, $msg);
		}
		return $messages;
	}
	/**
	 * Check have Error<br>
	 * Return true or false if have error or not.
	 * @return true/false
	 */
	public function hasError(){
		return (count($this->_error)>0) ? true : false;
	}
	/**
	 * @return String. Name of table.
	 */
	public function getDisplayName(){
		return "";
	}
}